package cn.edu.dlut.ssdut.foodCredit2012.service;

/*import java.util.List;

import org.hibernate.Query;*/
import org.hibernate.Session;

import cn.edu.dlut.ssdut.foodCredit2012.model.Basic;
import cn.edu.dlut.ssdut.foodCredit2012.model.BasicCollection;

import cn.edu.dlut.ssdut.foodCredit2012.util.HibernateUtil;

public class BasicService {
	
	public void add(Basic b) throws Exception{
		if(!hasBasic(b)){
			Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.save(b);
			session.getTransaction().commit();
		}
	}
	
	public void delete(Basic b) throws Exception{
		if(hasBasic(b)){
			Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.delete(b);
			session.getTransaction().commit();
		}
	}
	
	public void update (Basic b) throws Exception{
		if(hasBasic(b)){
			Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.update(b);
			session.getTransaction().commit();
		}
	}
	
	/*public List basicList() {
		List basicList = null;
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query query = session.createQuery("from Basic");
		query.setFirstResult(2);
		query.setMaxResults(10);
		basicList = query.list();
		
		session.getTransaction().commit();
		
		return basicList;
	}*/
	
	public boolean hasBasic(Basic b) throws Exception {
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		long count = (Long)session.createQuery("select count(*) from Basic b where b.licenceID = :licenceID")
			.setString("licenceID", b.getLicenceID())
			.uniqueResult();
		session.getTransaction().commit();
		
		if(count > 0) return true;
		return false;
		
	}
	
	public Basic load(Basic b) throws Exception{
		if(hasBasic(b)){
			b = loadByLicenceID(b.getLicenceID());
			return b;
		}
		return b;
	}
	
	public Basic loadByLicenceID(String licenceID){
		Basic b = new Basic();
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		b = (Basic)session.createQuery("select b from Basic b where b.licenceID = :licenceID")
			.setParameter("licenceID", licenceID)
			.uniqueResult();
		
		session.getTransaction().commit();
		
		return b;
	}
	
	public Basic basicCollectionToBasic(BasicCollection basicC)throws Exception{
		Basic b = new Basic();
		b.setAddr(basicC.getAddr());
		b.setAnnualSurveyDate(basicC.getAnnualSurveyDate());
		b.setAunualSurveyResult(basicC.getAunualSurveyResult());
		b.setAuthorizedAgency(basicC.getAuthorizedAgency());
		b.setBusinessItemBeginDate(basicC.getBusinessItemBeginDate());
		b.setBusinessItemEndDate(basicC.getBusinessItemEndDate());
		b.setBusinessScope(basicC.getBusinessScope());
		b.setLicenceID(basicC.getLicenceID());
		b.setManageID(basicC.getManageID());
		b.setName(basicC.getName());
		b.setOrganizationID(basicC.getOrganizationID());
		b.setType(basicC.getType());
		
		return b;
	}

}
